<?php
/**
 * Created by PhpStorm.
 * User: yanbin
 * Date: 2021/5/11
 * Time: 9:38 AM
 */

namespace App\Http\Middleware;


use App\Libs\Blogger;
use Closure;
use App\Libs\Encryption;

class VerifyLogin
{
    /**
     * @param $request
     * @param Closure $next
     * @return \Illuminate\Http\JsonResponse|mixed
     */
    public function handle($request, Closure $next)
    {
        $token = $request->header('token') ?? $request->input('token');
        if (empty($token)) {
            return output(0, '请先登录', null, 10008);
        }
        //解密token
        $trueToken = Encryption::getLoginToken($token);

        if (empty($trueToken)) {
            return output(0, '数据异常', null, 10008);
        }
        $expireTime = $trueToken['currentTime'] + $trueToken['expire'];
        if ($expireTime <= time()) {
            return output(0, '数据已经过期', null, 10008);
        }
        //获取用户信息
        if (!isset($trueToken['sub'])) {
            return output(0, '数据已经过期', null, 10008);
        }
        if (empty($userInfoVerify)) {
            return output(0, '请先登录', null, 10008);
        }
        $request->attributes->add(compact('userInfoVerify'));

        return $next($request);
    }
}